package com.haofengsoft.scce;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.haofengsoft.scce.event.MainEvent;
import com.haofengsoft.scce.ui.fragment.HomeFragment;
import com.haofengsoft.scce.ui.fragment.InformationFragment;
import com.haofengsoft.scce.ui.fragment.MineFragment;
import com.haofengsoft.scce.ui.fragment.OptionalFragment;
import com.haofengsoft.scce.ui.fragment.QuotationsFragment;
import com.haofengsoft.scce.ui.fragment.TradeFragment;

import de.greenrobot.event.EventBus;

/**
 * 主页
 *
 * @author zhj
 */
public class MainActivity extends FragmentActivity {

    private RelativeLayout mainTabHomeRl;// 首页
    private RelativeLayout mainTabOptionalRl;// 自选
    private RelativeLayout mainTabInformationRl;// 咨询
    private RelativeLayout mainTabQuotationsRl;// 行情
    private RelativeLayout mainTabTradeRl;// 交易
    private RelativeLayout mainTabMineRl;// 我

    private ImageView mainTabHomeIv;
    private ImageView mainTabOptionalIv;
    private ImageView mainTabInformationIv;
    private ImageView mainTabQuotationsIv;
    private ImageView mainTabTradeIv;
    private ImageView mainTabMineIv;

    private TextView mainTabHomeTv;
    private TextView mainTabOptionalTv;
    private TextView mainTabInformationTv;
    private TextView mainTabQuotationsTv;
    private TextView mainTabTradeTv;
    private TextView mainTabMineTv;

    private FragmentManager fragmentManager;

    private HomeFragment homeFragment;
    private OptionalFragment optionalFragment;
    private InformationFragment informationFragment;
    private QuotationsFragment quotationsFragment;
    private TradeFragment tradeFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initTabView();
        fragmentManager = getSupportFragmentManager();
        setTabSelection(0);
        //注册EventBus
        EventBus.getDefault().register(this);
    }

    private void initTabView() {
        mainTabHomeRl = (RelativeLayout) findViewById(R.id.main_tab_home_rl);
        mainTabOptionalRl = (RelativeLayout) findViewById(R.id.main_tab_optional_rl);
        mainTabInformationRl = (RelativeLayout) findViewById(R.id.main_tab_information_rl);
        mainTabQuotationsRl = (RelativeLayout) findViewById(R.id.main_tab_quotations_rl);
        mainTabTradeRl = (RelativeLayout) findViewById(R.id.main_tab_trade_rl);
        mainTabMineRl = (RelativeLayout) findViewById(R.id.main_tab_mine_rl);
        mainTabHomeIv = (ImageView) findViewById(R.id.main_tab_home_iv);
        mainTabOptionalIv = (ImageView) findViewById(R.id.main_tab_optional_iv);
        mainTabInformationIv = (ImageView) findViewById(R.id.main_tab_information_iv);
        mainTabQuotationsIv = (ImageView) findViewById(R.id.main_tab_quotations_iv);
        mainTabTradeIv = (ImageView) findViewById(R.id.main_tab_trade_iv);
        mainTabMineIv = (ImageView) findViewById(R.id.main_tab_mine_iv);
        mainTabHomeTv = (TextView) findViewById(R.id.main_tab_home_tv);
        mainTabOptionalTv = (TextView) findViewById(R.id.main_tab_optional_tv);
        mainTabInformationTv = (TextView) findViewById(R.id.main_tab_information_tv);
        mainTabQuotationsTv = (TextView) findViewById(R.id.main_tab_quotations_tv);
        mainTabTradeTv = (TextView) findViewById(R.id.main_tab_trade_tv);
        mainTabMineTv = (TextView) findViewById(R.id.main_tab_mine_tv);
        mainTabHomeRl.setOnClickListener(new TabClickListener(0));
        mainTabOptionalRl.setOnClickListener(new TabClickListener(1));
        mainTabInformationRl.setOnClickListener(new TabClickListener(2));
        mainTabQuotationsRl.setOnClickListener(new TabClickListener(3));
        mainTabTradeRl.setOnClickListener(new TabClickListener(4));
        mainTabMineRl.setOnClickListener(new TabClickListener(5));
    }

    class TabClickListener implements OnClickListener {

        private int selection;

        public TabClickListener(int selection) {
            this.selection = selection;
        }

        @Override
        public void onClick(View view) {
            setTabSelection(selection);
        }

    }

    private void refreshMenuAll() {
        mainTabHomeIv.setImageResource(R.drawable.main_home_icon_nor);
        mainTabOptionalIv.setImageResource(R.drawable.main_optional_icon_nor);
        mainTabInformationIv
                .setImageResource(R.drawable.main_information_icon_nor);
        mainTabQuotationsIv
                .setImageResource(R.drawable.main_quotations_icon_nor);
        mainTabTradeIv.setImageResource(R.drawable.main_trade_icon_nor);
        mainTabMineIv.setImageResource(R.drawable.main_mine_icon_nor);

        mainTabHomeTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));
        mainTabOptionalTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));
        mainTabInformationTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));
        mainTabQuotationsTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));
        mainTabTradeTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));
        mainTabMineTv.setTextColor(getResources().getColor(
                R.color.common_default_tv_nor));

    }

    public void onEventMainThread(MainEvent event) {
        switch (event.getOperType()) {
            case MainEvent.CONSULTATION: {
                //咨询
                setTabSelection(2);
                break;
            }
            case MainEvent.OPTIONAL: {
                setTabSelection(1);
                break;
            }
            case MainEvent.QUOTATION: {
                setTabSelection(3);
                break;
            }
            case MainEvent.TRADING: {
                setTabSelection(4);
                break;
            }
        }
    }

    private void setTabSelection(int selection) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        refreshMenuAll();
        switch (selection) {
            case 0: {
                mainTabHomeIv.setImageResource(R.drawable.main_home_icon_pre);
                mainTabHomeTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.main_body_fl, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            }
            case 1: {
                mainTabOptionalIv
                        .setImageResource(R.drawable.main_optional_icon_pre);
                mainTabOptionalTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (optionalFragment == null) {
                    optionalFragment = new OptionalFragment();
                    transaction.add(R.id.main_body_fl, optionalFragment);
                } else {
                    transaction.show(optionalFragment);
                }
                break;
            }
            case 2: {
                mainTabInformationIv
                        .setImageResource(R.drawable.main_information_icon_pre);
                mainTabInformationTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (informationFragment == null) {
                    informationFragment = new InformationFragment();
                    transaction.add(R.id.main_body_fl, informationFragment);
                } else {
                    transaction.show(informationFragment);
                }
                break;
            }
            case 3: {
                mainTabQuotationsIv
                        .setImageResource(R.drawable.main_quotations_icon_pre);
                mainTabQuotationsTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (quotationsFragment == null) {
                    quotationsFragment = new QuotationsFragment();
                    transaction.add(R.id.main_body_fl, quotationsFragment);
                } else {
                    transaction.show(quotationsFragment);
                }
                break;
            }
            case 4: {
                mainTabTradeIv.setImageResource(R.drawable.main_trade_icon_pre);
                mainTabTradeTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (tradeFragment == null) {
                    tradeFragment = new TradeFragment();
                    transaction.add(R.id.main_body_fl, tradeFragment);
                } else {
                    transaction.show(tradeFragment);
                }
                break;
            }
            case 5: {
                mainTabMineIv.setImageResource(R.drawable.main_mine_icon_pre);
                mainTabMineTv.setTextColor(getResources().getColor(
                        R.color.common_default_tv_pre));
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.main_body_fl, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                break;
            }
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (optionalFragment != null) {
            transaction.hide(optionalFragment);
        }
        if (informationFragment != null) {
            transaction.hide(informationFragment);
        }
        if (quotationsFragment != null) {
            transaction.hide(quotationsFragment);
        }
        if (tradeFragment != null) {
            transaction.hide(tradeFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
    }

    private static long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }

}
